
### Supplemental material for 
### "It sounds like they're moving: understanding and modelling emphasis-based policy change"
### Thomas M. Meyer & Markus Wagner, University of Vienna

###  row and column effects (RC) association model
###  following Lowe (2013) o distinguish emphasis- and position-based policy change
###  Data:see Fig 1 & Appendix A in the paper


setwd("*YOURWORKINGDIRECTORY*")

rm(list = ls(all = TRUE))
install.packages('foreign') 
install.packages('gnm') 

library(foreign)
library(gnm)


set.seed(1000)

data <- read.dta(paste("data_fig1.dta", sep=""), convert.factors=F)


### prepare data
count <- c(data$left,data$right)
leftright <- c("left", "right")
length <- length(data[,1])
row <- gl(2, length, labels = leftright)
col <- gl(length, 1, length = 2*length)

F <- data.frame(count, row,col)

RC1model <- gnm(count ~ row + col + Mult(row, col), family = poisson, data = F)

# see Goodman 1985 - equations (2.14)
rowProbs <- with(F, tapply(count, row, sum)/sum(count))
colProbs <- with(F, tapply(count, col, sum)/sum(count))
rowScores <- coef(RC1model)[((1+1+length-1)+1):(1+1+length+1)]
colScores <- coef(RC1model)[((1+1+length+1)+1):(1+1+length-1+2+length)]
rowScores <- rowScores - sum(rowScores * rowProbs)
colScores <- colScores - sum(colScores * colProbs)
beta1 <- sqrt(sum(rowScores^2 * rowProbs))
beta2 <- sqrt(sum(colScores^2 * colProbs))

# identification of eta and psi (row and column probabalisies)
eta <- c(0,coef(RC1model)[(1+1):(1+1)])
eta <- eta-sum(eta)/2
psi <- c(0,coef(RC1model)[((1+1)+1):((1+1)+length-1)])
psi <- psi-sum(psi)/length

nu <- rowScores/beta1
phi <- colScores/beta2 

# scaling: estimates for "left" to left pole
pos <- log((data$right)/(data$left))
cor(pos,phi)
if (cor(pos, phi)<0) {
	 phi <- (-1)* phi
	 nu <- (-1)* nu
} 


### save data
lambda <- RC1model$coeff[1]
beta <- beta1*beta2

eta1 <- eta[1]
eta2 <- eta[2]
nu1 <- nu[1]
nu2 <- nu[2]


data_out <- data.frame(data,lambda,beta,eta1,eta2,nu1,nu2,psi,phi)
write.csv(data_out,"Appendix_A.csv", row.names = FALSE)


